package business;

import java.util.ArrayList;

import persistence.*;

/*
 * Singleton Class
 */
public class CustomerBook 
{
	//member variables
	private ArrayList<Customer> Customers;
	private static CustomerBook Instance;
	
	private CustomerBook()
	{
		Customers = new ArrayList<Customer>(30);
		LoadFromStorage();
	}
	
	//methods
	/*
	 * Singleton Class Method
	 */
	public static CustomerBook GetInstance()
	{
		if(Instance==null)
		{
			Instance = new CustomerBook();
			return Instance;
		}
		
		else
		{
			return Instance;
		}
	}
	
	/*
	 * 
	 */
	private void LoadFromStorage()
	{	
		ArrayList<CustomerFascade> temp;
		temp = CustomerTDG.FindAll();
		
		Customer.setID_Counter(CustomerFascade.getID_Counter());

		for(CustomerFascade CF : temp)
		{
			Customers.add(new Customer(CF.getID(),CF.getLastName(),CF.getFirstName(),CF.getTelephone())); 
		}		
	}
	
	public Customer Find(int ID)
	{
		for(Customer C : Customers)
		{
			if(C.getID()==ID)
			{
				return C;
			}
		}
		
		return null;
	}

	public ArrayList<Customer> getCustomers() {
		return Customers;
	}

	public void setCustomers(ArrayList<Customer> customers) {
		Customers = customers;
	}

	public static CustomerBook getInstance() {
		return Instance;
	}

	public static void setInstance(CustomerBook instance) {
		Instance = instance;
	}
	
	
	
}
